﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class VendorProcessor : BaseProcessor {

        private int _pageSize;
        private VendorRepository _vendorRepository;

        public VendorProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._vendorRepository = new VendorRepository(pageSize);
        }

        public Vendor FetchVendorById(int vendorId) {
            try {
                return this._vendorRepository.FetchVendorById(vendorId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定供应商资料失败！");
            }
        }

        public List<Vendor> FetchVendorList() {
            try {
                return this._vendorRepository.FetchVendorList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取供应商列表失败！");
            }
        }

        public List<VendorViewModel> FetchVendorViewModelList(VendorSearchModel searchModel, int pageIndex = 1) {
            try {
                List<Vendor> resultList = this._vendorRepository.FetchVendorList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new VendorViewModel() {
                        Key = x.VendorId,
                        Code = x.Code,
                        Name = x.Name,
                        NameExtend = x.NameExtend,
                        Address = x.Address,
                        Linkman = x.Linkman,
                        Phone = x.Phone,
                        Purchaser = x.User.Name,
                        PayCurrency = x.Basic.Code,
                        VendorStatus = x.Basic1.Code,
                        VendorType = x.Basic2.Code,
                        Remarks = x.Remarks,
                        PayCondition = x.PayCondition,
                        AccountNumber = x.AccountNumber,
                        IncreaseRate = x.IncreaseRate,
                        InvoiceRate = x.InvoiceRate,
                        Fax = x.Fax,
                        Email = x.Email,
                        IsEnabled = x.IsEnabled,
                        IsAttach = x.AttachmentId.HasValue,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取供应商列表失败！");
            }
        }

        public void Insert(Vendor vendor) {
            try {
                vendor.VendorId = Guid.NewGuid().GetHashCode();
                vendor.CreatedDatetime = DateTime.Now;
                this._vendorRepository.Insert(vendor);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新供应商失败！");
            }
        }

        public void Update(Vendor vendor) {
            try {
                Vendor oldVendor = this.FetchVendorById(vendor.VendorId);
                if (!oldVendor.RowVersion.SequenceEqual(vendor.RowVersion)) {
                    throw this.BuildException("更新供应商失败，数据不一致性错误！");
                }
                vendor.ModifiedDatetime = DateTime.Now;
                this._vendorRepository.Update(vendor);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新供应商失败！");
            }
        }

        public void Delete(Vendor vendor) {
            try {
                this._vendorRepository.Delete(vendor);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除供应商失败！");
            }
        }

        public void Delete(params int[] vendorIds) {
            try {
                this._vendorRepository.DeleteMultipleVendor(vendorIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除供应商失败！");
            }
        }
    }
}
